package com.linkage.order.web;

import java.util.Date;
import java.util.List;
/**
 * @author Xiamingyu
 */




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.linkage.book.pojo.Book;
import com.linkage.book.pojo.OrderFormBook;
import com.linkage.order.interceptor.NeedLogin;
import com.linkage.order.pojo.Order;
import com.linkage.order.service.OrderService;
/**
 * @author XiaMingYu  
 */
@Controller
@RequestMapping("/")
public class OrderController {
	@Autowired
	private OrderService orderService;
    private Order order;
    private Book book;
    private OrderFormBook orderFormBook;
	@RequestMapping(value = "/orderlist", method = RequestMethod.GET)
	@NeedLogin
	public String orderList(Model model) {
		List<Order> orders = this.orderService.queryAll();
		model.addAttribute("orders", orders);
		return "/orderlist";
		}
	
	@RequestMapping(value = "/admin/orderlist", method = RequestMethod.GET)
	@NeedLogin
	public String aorderList(Model model) {
		String orderId = this.order.getorderId();
		Date orderTime = this.order.getorderTime();
		double total =this.order.getTotal();
		int status = this.order.getStatus();
		List<Order> orders = this.orderService.query(orderId,orderTime, total,status);
		model.addAttribute("orders", orders);
		return "/admin/orderlist";
		}
	
	@RequestMapping(value = "/user/orderlist", method = RequestMethod.GET)
	@NeedLogin
	public String uorderList(Model model) {
		String orderId = this.order.getorderId();
		Date orderTime = this.order.getorderTime();
		double total =this.order.getTotal();
		int status = this.order.getStatus();
		double subTotal = this.orderService.calTotal(this.book.getbookId(), this.orderFormBook.getBookIdQuantity());
		List<Order> orders = this.orderService.queryItem(orderId,orderTime,total,status,subTotal);
		model.addAttribute("orders", orders);
		return "/admin/orderlist";
		}
		
		@RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
		public String saveOrder(Order order){
			this.orderService.saveOrder(order);
			return "redirect:/orderlist";
		}
		
		@RequestMapping(value = "/deleteOrder/{orderId}", method = RequestMethod.GET)
		@NeedLogin
		public String delete(Order order, @PathVariable("orderId") String orderId) {
			this.orderService.deleteOrder(orderId);
			return "redirect:/orderlist";
		}
		
		@RequestMapping(value = "/payOrder/{orderId}", method = RequestMethod.GET)
		@NeedLogin
		public void payOrder(@PathVariable("orderId") String orderId,@PathVariable("userId") int userId){
			this.orderService.payOrder(orderId,userId);
		}
	}

